package com.example.androidproject.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidproject.R;
import com.example.androidproject.app.ClassifyActivity3;
import com.example.androidproject.movie.adapter.BannerImageAdapter1;
import com.example.androidproject.movie.adapter.ChannelAdapter;
import com.example.androidproject.novel.bean.NovelBean;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class RecycelViewAdapter extends RecyclerView.Adapter {
    private final String TAG = "GGSTUDY";
    private static final int BANNER = 0;
    private static final int CHANNEL = 1;

    private int currentType = BANNER;
    private final LayoutInflater layoutInflater;
    private final Context mContext;
    private final List<NovelBean.DataBean> NovelData;
    private final int TypeFlag;

    public RecycelViewAdapter(Context mContext, ArrayList<NovelBean.DataBean> data, int TypeFlag) {
        this.mContext = mContext;
        this.NovelData = data;
        this.TypeFlag = TypeFlag;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(mContext, layoutInflater.inflate(R.layout.banner_viewpapger, null));
        }
        if (viewType == CHANNEL) {
            return new ChannelViewHolder(mContext, layoutInflater.inflate(R.layout.channel_layout, null));
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == BANNER) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            if (NovelData != null) {
                bannerViewHolder.setData();
            }
        }
        if (getItemViewType(position) == CHANNEL) {
            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
            channelViewHolder.setData();
        }

    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private final Context mContext;
        private final Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.banner = itemView.findViewById(R.id.banner);
        }

        public void setData() {
            List<String> urlList = new ArrayList<>();
            if (TypeFlag == 2) {
                for (int i = 0; i < NovelData.size(); i++) {
                    String imageUrl = NovelData.get(i).getImage();
                    urlList.add(imageUrl);
                }
            }
            banner.addBannerLifecycleObserver((LifecycleOwner) mContext)
                    .setIndicator(new CircleIndicator(mContext))
                    .setBannerRound(30)
                    .setBannerGalleryMZ(90, 0.6f)
                    .setAdapter(new BannerImageAdapter1(urlList, mContext))
                    .start();
        }
    }

    class ChannelViewHolder extends RecyclerView.ViewHolder {
        private final Context mContext;
        private final GridView gv_channel;

        public ChannelViewHolder(Context mContext, View viewItem) {
            super(viewItem);
            this.mContext = mContext;
            this.gv_channel = viewItem.findViewById(R.id.gv_channel);
        }

        public void setData() {
            List<String> textList = new ArrayList<>();
            textList.add("分类");
            textList.add("收藏");
            ChannelAdapter adapter = new ChannelAdapter(mContext, textList);
            gv_channel.setAdapter(adapter);
            gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent(mContext, ClassifyActivity3.class);
                    switch (position) {
                        case 0:
                            if (TypeFlag == 2) {
                                bundle.putInt("flag", 2);
                            }
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                            break;
                        case 1:
                            break;
                        default:
                            Toast.makeText(mContext, "channel点击了：" + position, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }


    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
        }
        return currentType;
    }

    @Override
    public int getItemCount() {
        return 2;
    }
}
